#include "chooselevelscene.h"
#include<QMenuBar>
#include<QPainter>
#include<QDebug>
#include<mypushbutton.h>
#include<QTimer>
#include<QLabel>
#include<QSoundEffect>
ChooseLevelScene::ChooseLevelScene(QWidget *parent)
    : QMainWindow{parent}
{
    //配置选择关卡场景
    this->setFixedSize(320,588);
    //设置图标
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    //设置标题
    this->setWindowTitle("请选择关卡");

    QMenuBar * bar = menuBar();
    setMenuBar(bar);

    //创建开始菜单
    QMenu * startMenu = bar->addMenu("开始");
//    创建退出菜单项
    QAction * quitAction = startMenu->addAction("退出游戏");


    //选择关卡音效
    QSoundEffect * chooseSound = new QSoundEffect(this);
    chooseSound->setSource(QUrl::fromLocalFile("://res/TapButtonSound.wav"));
    //返回按钮音效
    QSoundEffect * backSound = new QSoundEffect(this);
    backSound->setSource(QUrl::fromLocalFile("://res/TapButtonSound.wav"));


//    点击退出 退出游戏
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });
    //返回按钮

    MypushButton * backBtn = new MypushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backBtn ->setParent(this);
    backBtn ->move(this->width() - backBtn->width(),this->height() - backBtn->height());

//    点击返回按钮实现返回
    connect(backBtn,&MypushButton::clicked,[=](){
        qDebug() << "点击了返回按钮";
        backSound->play();
        //告诉主场景返回了   主场景监听  Chooselevelscene的返回按钮
//        延时操作
        QTimer::singleShot(800,this,[=](){
            //发出自定义chooseSceneBack信号
            emit this->chooseSceneBack();
        });

    });


    for(int i = 0; i < 20; i ++)
    {
        MypushButton * menuBtn = new MypushButton(":/res/LevelIcon.png");
        menuBtn->setParent(this);
        menuBtn->move(25 + i % 4 * 70,130 + i / 4 * 70);

        //menuBtn->setText(QString::number(i + 1));


//        监听每个按钮的点击事件
        connect(menuBtn,&MypushButton::clicked,[=](){
            chooseSound->play();
            QString str = QString("选择的是第 %1 关").arg(i + 1);
            qDebug() << str;

//            进入游戏场景
            this->hide();//将选关卡隐藏掉
            play = new PlayScene(i + 1);// 创建游戏场景
            play->setGeometry(this->geometry());
            play->show();//显示游戏场景

//            监听游戏场景的返回按钮信号
            connect(play,&PlayScene::chooseSceneBack,[=](){
                chooseSound->play();
                this->setGeometry(play->geometry());
                this->show();
                delete play;
                play = NULL;
            });
        });


        QLabel * label = new QLabel;
        label->setParent(this);

        label->setFixedSize(menuBtn->width(),menuBtn->height());
        label->setText(QString::number(i + 1));
        label->move(25 + i % 4 * 70,130 + i / 4 * 70);

//        设置label对齐方式   水平剧中加  垂直剧中
        label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
//        设置 让鼠标能够穿透 51号
        label->setAttribute(Qt::WA_TransparentForMouseEvents);
    }
}
void ChooseLevelScene::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/OtherSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //    加载标题
    pix.load(":/res/Title.png");
    painter.drawPixmap((this->width() - pix.width() ),30,pix.width() * 0.5,pix.height(),pix);

}


















